<template>
  <div class="contentView" v-loading="formLoading">
    <div class="btn">
      <el-button 
        type="primary"
        class="mr-5px" 
        v-if="formType === 'update'" 
        @click="submitForm"
        :disabled="formLoading"
        v-hasPermi="['technology:release-task-ticket:release']"
        >发 布</el-button
      >
      <el-button @click="goBack">关 闭</el-button>
    </div>
    <detail ref="detailRef" :style="`overflow-y: auto; height: ${contentHeight}px`" :detailData="formData" />
  </div>
</template>
<script setup lang="ts">
import * as DesignCompleteTicketApi from '@/api/technology/designCompleteTicket'
import { useTagsViewStore } from '@/store/modules/tagsView'
import detail from '../components/completionOrder/index.vue'

const message = useMessage() // 消息弹窗
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：view - 查看；update - 修改
const formData = ref({
  id: undefined,
  ticketCode: undefined,  //完工单号
  taskCode: undefined,  //任务单号
  productType: undefined,  //产品类型
  productCode: undefined, //产品编号
  componentName: undefined, //部件名称
  drawingType: undefined, //出图类型
  drawingContent: undefined,  //出图内容
  changeContent: undefined, //改动内容
  alerts: [], //图纸改动数量
  remarks: undefined, //备注
  dictTicketStatus: undefined,  //任务状态
  verifyOpinion: undefined, //校对意见
  verifior: undefined,
  verifiorName: undefined,  //校对人姓名
  verifiorTime: undefined,  //校对时间
  verifyTime: undefined,
  auditorName: undefined, //审核人
  auditOpinion: undefined,  //审核意见
  auditorTime: undefined, //审核时间
  auditor: undefined,
  auditTime: undefined,
  ratifyResult: undefined,  //批准结果
  ratifiorName: undefined,  //批准人
  ratifior: undefined,
  ratifyTime: undefined,  //批准时间
  customerCode: undefined,  //客户代码
  productSpec: undefined  //产品规格
})
const contentHeight = ref(0)

/** 提交表单 */
const detailRef = ref()
const submitForm = async () => {
  await ElMessageBox({
    showCancelButton: true,
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    message: h('div', null, [
      h('div', { style: 'text-align: center' }, '系统将自动将图号清单转换为图纸清单。'),
      h('div', { style: 'text-align: center; color: red; margin: 10px 0' }, '请先在PDM中上传对应图号文件与上传BOM清单后再确认发布。'),
      h('div', { style: 'text-align: center' }, '是否确认发布？'),
    ]),
  })
  let resData = await detailRef.value.formData
  formLoading.value = true
  try {
    const data = {
      id: resData.id,
      dictTaskType: resData.dictTaskType
    }
    await DesignCompleteTicketApi.releaseCompleteTicket(data)
    message.success('发布成功')
    goBack()
  } finally {
    formLoading.value = false
  }
}


/**返回 */
const tagsViewStore = useTagsViewStore()
const route = useRouter() // 路由
const goBack = async () => {
  //获取当前路由并删除tagView
  tagsViewStore.delView(route.currentRoute.value)
  //跳转列表页
  route.push({
    path: '/technology/technology/drawingProduction/releaseTaskTicket'
  })
}

// 获取数据
const { query } = useRoute() // 路由的查询
const getData = async () => {
  formLoading.value = true
  formType.value = (query as any).type
  try {
    if (query.id) {
      formData.value = await DesignCompleteTicketApi.getDesignCompleteTicket((query as any).id)
    }
  } finally {
    formLoading.value = false
  }
}

/** 初始化 */
onMounted(async () => {
  contentHeight.value = document.body.clientHeight - 132 - 57
  getData()
})
</script>
<style lang="scss" scoped>
.btn {
  margin-bottom: 10px;
}

.routeHeader {
  width: 100%;
  border-top: 1px solid #969696;
  border-bottom: 1px solid #969696;
  display: flex;
  align-items: center;
  padding: 10px 0;
}
</style>